package com.hzz.web.app.river;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.hzz.api.river.IDrainOutletService;
import com.hzz.api.river.IIntakeInfoService;
import com.hzz.api.river.IPumpGateService;
import com.hzz.api.river.IRiverBillboardService;
import com.hzz.api.river.IRiverInfoService;
import com.hzz.api.river.IRiverPolicyService;
import com.hzz.api.river.ISewageTreatmentService;
import com.hzz.api.river.IStationService;
import com.hzz.api.river.IWaterFunctionInfoService;
import com.hzz.api.river.IWaterShorelineService;
import com.hzz.api.user.IUserService;
import com.hzz.dto.river.ApiRiverArchiveDto;
import com.hzz.dto.river.DrainOutletDto;
import com.hzz.dto.river.IntakeInfoDto;
import com.hzz.dto.river.PumpGateDto;
import com.hzz.dto.river.RiverBillboardDto;
import com.hzz.dto.river.RiverInfoDto;
import com.hzz.dto.river.RiverPolicyDto;
import com.hzz.dto.river.SewageTreatmentDto;
import com.hzz.dto.river.StationDto;
import com.hzz.dto.river.WaterFunctionInfoDto;
import com.hzz.dto.river.WaterShorelineDto;
import com.hzz.dto.user.UserDto;
import com.hzz.framework.PageUtil;
import com.hzz.framework.Response;
import com.hzz.framework.annotation.NoAuthorize;
import com.hzz.framework.util.CommonConstant;
import com.hzz.framework.web.controller.BaseController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

@Api(tags = "河道信息APP接口")
@RestController
@Slf4j
public class ApiRiverInfoController extends BaseController {
    static final String acPrefix = "/api/river/riverInfo/";
    
    @Autowired
    IRiverInfoService riverInfoService;
    
    @Autowired
    IUserService userService;
    
    @Autowired
    IRiverPolicyService riverPolicyService;

	@Autowired
	IDrainOutletService drainOutletService;
	@Autowired
	IPumpGateService pumpGateService;
	@Autowired
	IStationService stationService;
	@Autowired
	IWaterFunctionInfoService waterFunctionInfoService;
	@Autowired
	IIntakeInfoService intakeInfoService;
	@Autowired
	ISewageTreatmentService sewageTreatmentService;
	@Autowired
	IWaterShorelineService waterShorelineService;
	@Autowired
	IRiverBillboardService riverBillboardService;
    
	@NoAuthorize
    @ApiOperation(value = "河道列表")
    @RequestMapping(method = RequestMethod.GET, value = acPrefix + "list")
    public Response<List<RiverInfoDto>> list(@RequestParam(required = false, name = "userId") @ApiParam("河长ID,不传则返回所有河道") Long userId,
            @RequestParam(required = false, name = "areaIds") @ApiParam("行政区划，不传返回所有河道") String areaIds) {
        log.info("list.........");
        Response<List<RiverInfoDto>> result = new Response<>();
        try {
            RiverInfoDto param = new RiverInfoDto();
            param.setPrincipalId(userId == null ? null : userId + "");
            param.setAreaIds(org.apache.commons.lang3.StringUtils.isNotBlank(areaIds) ? areaIds : "");
            result.data = riverInfoService.findDataIsList(param);
        } catch (Exception e) {
            result = Response.error(e.getMessage());
        }
        return result;
    }

	@NoAuthorize
	@RequestMapping(method = { RequestMethod.GET, RequestMethod.POST }, value = acPrefix + "page/{pageNum}")
	@ApiOperation(value = "河道分页(未删除)")
	public Response page(@RequestBody RiverInfoDto dto, @PathVariable("pageNum") Integer pageNum) {
		log.info("page.........");
		Response result = new Response();
		try {
			dto.setPageSize(CommonConstant.PAGEROW_DEFAULT_COUNT);
			dto.setPageNum(pageNum);
			dto.setDelFlag(0);
			result.data = PageUtil.copy(riverInfoService.findDataIsPage(dto));
		} catch (Exception e) {
			result = Response.error(e.getMessage());
		}
		return result;
	}

	@NoAuthorize
    @RequestMapping(method = RequestMethod.GET, value = acPrefix + "archives/{id}")
    @ApiOperation(value = "一河一档")
    public Response<ApiRiverArchiveDto> archives(@ApiParam("河道ID") @PathVariable("id") Long id) {
        log.info("archives.........");
        Response<ApiRiverArchiveDto> result = new Response<>();
        try {
            RiverInfoDto dto = new RiverInfoDto() {
                {
                    setId(id);
                    setDelFlag(0);
                }
            };
            ApiRiverArchiveDto rdto = new ApiRiverArchiveDto();
            // 获取河道名录详情
            RiverInfoDto riverInfo = riverInfoService.findDataById(dto);
            rdto.setRiverInfo(riverInfo);
            
            if (riverInfo != null) {
                // 获取河长信息
                if (StringUtils.isNotBlank(riverInfo.getPrincipalId())) {
                    String userId = riverInfo.getPrincipalId().split(",")[0];
                    UserDto userDto = new UserDto();
                    userDto.setId(Long.parseLong(userId));
                    UserDto user = userService.findDataById(userDto);
                    rdto.setUser(user);
                }
                // 获取一河一策
                RiverPolicyDto policyDto = new RiverPolicyDto();
				policyDto.setRiverId(id);
                List<RiverPolicyDto> policyList = riverPolicyService.findDataIsList(policyDto);
				rdto.setRiverPolicys(policyList);
				/*
				 * if (policyList != null && policyList.size() > 0) {
				 * RiverPolicyDto policy = policyList.get(0);// 返回第一个 // 查询上传人姓名
				 * UserDto userDto = new UserDto();
				 * userDto.setId(policy.getCreateId()); UserDto user =
				 * userService.findDataById(userDto); policy.setCreateName(user
				 * == null ? null : user.getName());
				 * 
				 * rdto.setRiverPolicy(policy); }
				 */
            }
            result.setData(rdto);
        } catch (Exception e) {
            log.error("API查询一河一档异常！", e);
            result = Response.error(e.getMessage());
        }
        return result;
    }

	@NoAuthorize
	@RequestMapping(method = RequestMethod.GET, value = acPrefix + "riverInfo/{id}")
	@ApiOperation(value = "河道名录")
	public Response<RiverInfoDto> riverInfo(@ApiParam("河道ID") @PathVariable("id") Long id) {
		log.info("riverInfo.........");
		Response<RiverInfoDto> result = new Response<>();
		try {
			RiverInfoDto dto = new RiverInfoDto() {
				{
					setId(id);
					setDelFlag(0);
				}
			};
			// 获取河道名录详情
			RiverInfoDto riverInfo = riverInfoService.findDataById(dto);
			result.setData(riverInfo);
		} catch (Exception e) {
			log.error("API查询一河一档异常！", e);
			result = Response.error(e.getMessage());
		}
		return result;
	}

	@NoAuthorize
	@ApiOperation(value = "河道排污口列表")
	@RequestMapping(method = RequestMethod.GET, value = acPrefix + "drainOutletList/{riverId}")
	public Response<List<DrainOutletDto>> drainOutletList(@PathVariable("riverId") Long riverId) {
		log.info("drainOutletList.........");
		Response<List<DrainOutletDto>> result = new Response<>();
		try {
			DrainOutletDto dto = new DrainOutletDto();
			dto.setRiverId(riverId);
			result.data = drainOutletService.findDataIsList(dto);
		} catch (Exception e) {
			result = Response.error(e.getMessage());
		}
		return result;
	}

	@NoAuthorize
	@ApiOperation(value = "河道泵闸信息列表")
	@RequestMapping(method = RequestMethod.GET, value = acPrefix + "pumpGateList/{riverId}")
	public Response<List<PumpGateDto>> pumpGateList(@PathVariable("riverId") Long riverId) {
		log.info("pumpGateList.........");
		Response<List<PumpGateDto>> result = new Response<>();
		try {
			PumpGateDto dto = new PumpGateDto();
			dto.setRiverCourseCode("" + riverId);
			result.data = pumpGateService.findDataIsList(dto);
		} catch (Exception e) {
			result = Response.error(e.getMessage());
		}
		return result;
	}

	@NoAuthorize
	@ApiOperation(value = "河道测站信息列表")
	@RequestMapping(method = RequestMethod.GET, value = acPrefix + "stationList/{riverId}")
	public Response<List<StationDto>> stationList(@PathVariable("riverId") Long riverId) {
		log.info("stationList.........");
		Response<List<StationDto>> result = new Response<>();
		try {
			StationDto dto = new StationDto();
			dto.setRiverCourseCode("" + riverId);
			result.data = stationService.findDataIsList(dto);
		} catch (Exception e) {
			result = Response.error(e.getMessage());
		}
		return result;
	}

	@NoAuthorize
	@ApiOperation(value = "河道水功能区信息列表")
	@RequestMapping(method = RequestMethod.GET, value = acPrefix + "waterFunctionInfoList/{riverId}")
	public Response<List<WaterFunctionInfoDto>> waterFunctionInfoList(@PathVariable("riverId") Long riverId) {
		log.info("waterFunctionInfoList.........");
		Response<List<WaterFunctionInfoDto>> result = new Response<>();
		try {
			WaterFunctionInfoDto dto = new WaterFunctionInfoDto();
			dto.setRiverCourseCode("" + riverId);
			result.data = waterFunctionInfoService.findDataIsList(dto);
		} catch (Exception e) {
			result = Response.error(e.getMessage());
		}
		return result;
	}

	@NoAuthorize
	@ApiOperation(value = "河道取水口信息列表")
	@RequestMapping(method = RequestMethod.GET, value = acPrefix + "intakeInfoList/{riverId}")
	public Response<List<IntakeInfoDto>> intakeInfoList(@PathVariable("riverId") Long riverId) {
		log.info("intakeInfoList.........");
		Response<List<IntakeInfoDto>> result = new Response<>();
		try {
			IntakeInfoDto dto = new IntakeInfoDto();
			dto.setRiverId(riverId);
			result.data = intakeInfoService.findDataIsList(dto);
		} catch (Exception e) {
			result = Response.error(e.getMessage());
		}
		return result;
	}

	@NoAuthorize
	@ApiOperation(value = "河道污水处理设施列表")
	@RequestMapping(method = RequestMethod.GET, value = acPrefix + "sewageTreatmentsList/{riverId}")
	public Response<List<SewageTreatmentDto>> sewageTreatmentsList(@PathVariable("riverId") Long riverId) {
		log.info("sewageTreatmentsList.........");
		Response<List<SewageTreatmentDto>> result = new Response<>();
		try {
			SewageTreatmentDto dto = new SewageTreatmentDto();
			dto.setRiverId(riverId);
			result.data = sewageTreatmentService.findDataIsList(dto);
		} catch (Exception e) {
			result = Response.error(e.getMessage());
		}
		return result;
	}

	@NoAuthorize
	@ApiOperation(value = "河道岸线利用列表")
	@RequestMapping(method = RequestMethod.GET, value = acPrefix + "waterShorelineList/{riverId}")
	public Response<List<WaterShorelineDto>> waterShorelineList(@PathVariable("riverId") Long riverId) {
		log.info("waterShorelineList.........");
		Response<List<WaterShorelineDto>> result = new Response<>();
		try {
			WaterShorelineDto dto = new WaterShorelineDto();
			dto.setRiverCourseCode("" + riverId);
			result.data = waterShorelineService.findDataIsList(dto);
		} catch (Exception e) {
			result = Response.error(e.getMessage());
		}
		return result;
	}

	@NoAuthorize
	@ApiOperation(value = "河道河长公示牌列表")
	@RequestMapping(method = RequestMethod.GET, value = acPrefix + "riverBillboardList/{riverId}")
	public Response<List<RiverBillboardDto>> riverBillboardList(@PathVariable("riverId") Long riverId) {
		log.info("riverBillboardList.........");
		Response<List<RiverBillboardDto>> result = new Response<>();
		try {
			RiverBillboardDto dto = new RiverBillboardDto();
			dto.setRiverId(riverId);
			result.data = riverBillboardService.findDataIsList(dto);
		} catch (Exception e) {
			result = Response.error(e.getMessage());
		}
		return result;
	}
}